﻿using System;
using System.Collections.Generic;
using System.Text;

using WebKit;
using WebKit.DOM;
using System.Threading;
using Crawler_v1.utils;
using System.Windows.Forms;

namespace Crawler_v1.service.impl
{
    class WebKitCrawler2
    {

        protected string baseUrl = "http://fec.mofcom.gov.cn/article/fwydyl/";

        private string url;
        private int taskNum=0;
        private int total = 0;
        public WebKitBrowser WebKitBrowser1{get;set;}

        public delegate void task();

      
        event EventHandler MyDocumentCompleted;
        public event task onTaskFinish;

        /// <summary>
        /// 移除所有的事件绑定
        /// </summary>
        /// <param name="clearEvent"></param>
        private void clear_event(EventHandler clearEvent)
        {
            Delegate[] dels = clearEvent.GetInvocationList();
            foreach (Delegate d in dels)
            {
                /*--------------------------------------*/
                //得到方法名
                object delObj = d.GetType().GetProperty("Method").GetValue(d, null);
                string funcName = (string)delObj.GetType().GetProperty("Name").GetValue(delObj, null);

                Console.WriteLine(funcName);
                /*--------------------------------------*/
                MyDocumentCompleted -= d as EventHandler;
            }
        }

        public void addJs(string name)
        {
            Document document = WebKitBrowser1.Document;
            var head = document.GetElementsByTagName("head");

            var script = document.CreateElement("script");
            script.SetAttribute("type", "text/javascript");
            script.SetAttribute("src", "");
            script.TextContent = string.Format(";{0};", FileUtil.GetScript(name));//"crawler.js","jquery.js"

            head[0].AppendChild(script);
        }


        private Form form;
        public WebKitCrawler2(Form form)
        {
            this.form = form;
            this.WebKitBrowser1 = new WebKitBrowser();
            this.WebKitBrowser1.BackColor = System.Drawing.Color.White;
            this.WebKitBrowser1.Location = new System.Drawing.Point(0, 0);
            this.WebKitBrowser1.Size = new System.Drawing.Size(0, 0);
            this.WebKitBrowser1.Name = "webKitBrowser" + System.Guid.NewGuid().ToString();
            this.WebKitBrowser1.TabIndex = 0;
            this.WebKitBrowser1.Url = null;
            form.Controls.Add(this.WebKitBrowser1);
            WebKitBrowser1.DocumentCompleted += (s, e) =>
            {
                MyDocumentCompleted(s,e);
            };
        }

        public void init(string url)
        {
            WebKitBrowser1.Navigate(url);
            //clear_event(MyDocumentCompleted);
            MyDocumentCompleted = null;
            taskNum = 0;
            total = 0;
        }

         public void onGet(task task1) {
             total++;
             MyDocumentCompleted += (s, e) =>
             {
                 task1();
                 taskNum++;
                 if (total == taskNum)
                 {
                     onTaskFinish();
                 }
                 //var thread = new Thread(new ParameterizedThreadStart(completed));
                 //thread.Start("");//此线程用于通知数据已经获取完毕,执行线程将往下执行.
             };
         }

         ~WebKitCrawler2()
         {
             form.Controls.Remove(this.WebKitBrowser1);
         }
    }
}
